<?php

/* 通过数据库获取所有元素，通过下面函数构造树形结构 
 * ---------------------------------------------------- */  

function getTree($categorys){
	$id = 0;
	$level = 0;
	$categoryObjs = array();
	$tree = array();
	$childrenNodes = array();
	foreach($categorys as $cate){
		$obj = new stdClass();
		$obj->root = $cate;
		$id = $cate['catId'];
		$level = $cate['parentId'];
		$obj->children = array();
		$categoryObjs[$id] = $obj;
		if ($level) {
			$childrenNodes[] = $obj;
		} else {
			$tree[] = $obj;
		}
	}

	foreach($childrenNodes as $node){
		$cate = $node->root;
		$id = $cate['catId'];
		$level = $cate['parentId'];
		$categoryObjs[$level]->children[] = $node;

	}

	return $tree;
}

/* 通过数据库获取所有元素，格式化为Html树 
 * ---------------------------------------------------- */
 
 //商品分类
 function getTreeHtml($categorys,$html=""){
	foreach($categorys as $val){
		$html .= '<li class="list-group-item clearfix';
		if(!empty($val->children)){
			$html .= ' dropdown';
		}
		$html .= '">';
		if(!empty($val->children)){
			$html .= '<a href="javascript:void(0);">';
		}else{
			$html .= '<a href="'.U('Home/Index/goodsList',array('goodsCatId3'=>$val->root['catId'])).'">';
		}
		$html .= '<i class="fa fa-angle-right"></i>';		
		$html .= $val->root['catName'];
		if(!empty($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(!empty($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getTreeChild($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
}

function getTreeChild($categorys){
	foreach($categorys as $val){
		if(!empty($val->children)){
			$html .= '<li class="list-group-item dropdown clearfix">';
		}else{
			$html .= '<li>';
		}
		if(!empty($val->children)){
			$html .= '<a href="javascript:void(0);">';
		}else{
			$html .= '<a href="'.U('Home/Index/goodsList',array('goodsCatId3'=>$val->root['catId'])).'">';
		}
		$html .= '<i class="fa fa-circle"></i>';		
		$html .= $val->root['catName'];
		if(!empty($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(!empty($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getTreeChild($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
}


//文章分类
 function getTreeArticle($categorys,$html=""){
	foreach($categorys as $val){
		$html .= '<li class="list-group-item clearfix';
		if(!empty($val->children)){
			$html .= ' dropdown';
		}
		$html .= '">';
		if(!empty($val->children)){
			$html .= '<a href="javascript:void(0);">';
		}else{
			$html .= '<a href="'.U('Home/Index/aboutList',array('catId'=>$val->root['catId'])).'">';
		}
		$html .= '<i class="fa fa-angle-right"></i>';		
		$html .= $val->root['catName'];
		if(!empty($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(!empty($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getChildArticle($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
}

function getChildArticle($categorys){
	foreach($categorys as $val){
		if(!empty($val->children)){
			$html .= '<li class="list-group-item dropdown clearfix">';
		}else{
			$html .= '<li>';
		}
		if(!empty($val->children)){
			$html .= '<a href="javascript:void(0);">';
		}else{
			$html .= '<a href="'.U('Home/Index/aboutList',array('catId'=>$val->root['catId'])).'">';
		}
		$html .= '<i class="fa fa-circle"></i>';		
		$html .= $val->root['catName'];
		if(!empty($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(!empty($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getTreeChild($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
}
 
 
/* function getTreeHtml($categorys,$html=""){
	foreach($categorys as $val){
		$html .= '<li class="list-group-item clearfix';
		if(isset($val->children)){
			$html .= ' dropdown';
		}
		$html .= '">';
		$html .= '<a href="javascript:void(0);">';
		$html .= '<i class="fa fa-angle-right"></i>';		
		$html .= $val->root['catName'];
		if(isset($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(isset($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getTreeChild($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
}

function getTreeChild($categorys){
	foreach($categorys as $val){
		$html .= '<li class="list-group-item dropdown clearfix">';
		$html .= '<a href="javascript:void(0);">';
		$html .= '<i class="fa fa-circle"></i>';		
		$html .= $val->root['catName'];
		if(isset($val->children)){
			$html .= '<i class="fa fa-angle-down"></i>';
		}
		$html .= '</a>';
		if(isset($val->children)){
			$html .= '<ul class="dropdown-menu">';            
			$html .= getTreeChild($val->children);
			$html .= '</ul>';
		}		
		$html .= '</li>';
	}
	return $html;
} */



/* 分页格式化 
 * ---------------------------------------------------- */
function getPage($count, $pagesize = 0) {
    $page = new Think\Page($count, $pagesize);
    $page->setConfig('header','共 %TOTAL_ROW% 条记录&nbsp;第 %NOW_PAGE% 页/共 %TOTAL_PAGE% 页');
	$page->setConfig('prev','<i class="fa fa-angle-left"></i>');
	$page->setConfig('next','<i class="fa fa-angle-right"></i>');
	$page->setConfig('theme',"<ul class='pagination'>%HEADER%</ul><ul class='pagination pull-right'><li>%FIRST%</li> <li>%UP_PAGE%</li> <li>%LINK_PAGE%</li> <li>%DOWN_PAGE%</li>
<li>%END%</li></ul>");
	return $page;
}
?>
